package leecode;


public class No0002_AddTwoNumbers {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null;
        ListNode pre = null;
        ListNode l1Point = l1;
        ListNode l2Point = l2;
        int lastLeave = 0;
        while (l1Point != null || l2Point != null || lastLeave > 0) {
            int current = (l1Point == null ? 0 : l1Point.val) + (l1Point == null ? 0 : l2Point.val) + lastLeave;
            if (current >= 10) {
                current = current - 10;
                lastLeave = 1;
            } else {
                lastLeave = 0;
            }

            ListNode currentNode = new ListNode(current);
            if (head == null) {
                head = currentNode;
            }
            if (pre != null) {
                pre.next = currentNode;
            }
            pre = currentNode;
            l1Point = l1Point == null ? null : l1Point.next;
            l2Point = l2Point == null ? null : l2Point.next;
        }
        return head;
    }


    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}



